<?php
    class Admincp_Model_Sales extends Zend_Db_Table_Abstract
    {
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listSales()
        {
            $sql = $this->_db->select()
                            ->from('sales')
                            ->order('id DESC');
            return $sql;
        }
        
        public function listHotelByType($type_hotel)
        {
            $sql = $this->_db->select();
            switch($type_hotel) {
                case    1   :   {
                    $sql->from('hotel_language', array('hotel_id AS id', 'name'));
                    break;
                }
                case    2   :   {
                    $sql->from('boating_language', array('boating_id AS id', 'name'));
                    break;
                }
                case    3   :   {
                    $sql->from('resort_language', array('resort_id AS id', 'name'));
                    break;
                }
            }
            $sql->where('language_id = ?', 5)
                ->order('name ASC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function getSalesById($id)
        {
            $sql = $this->_db->select()
                            ->from('sales')
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getSalesLanguageById($id)
        {
            $sql = $this->_db->select()
                            ->from('sales_language AS s')
                            ->join('language AS l', 's.language_id = l.id', array('locale'))
                            ->where('s.sales_id = ?', $id);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function getHotel($type, $type_id)
        {
            $sql = $this->_db->select();
            switch($type) {
                case    1   :   {
                    $sql->from('hotel AS h', array('image'))
                        ->join('hotel_language AS hl', 'hl.hotel_id = h.id', array('name'))
                        ->where('h.id = ?', $type_id);
                    break;
                }
                case    2   :   {
                    $sql->from('boating AS b', array('image'))
                        ->join('boating_language AS bl', 'bl.boating_id = b.id', array('name'))
                        ->where('b.id = ?', $type_id);
                    break;
                }
                case    3   :   {
                    $sql->from('resort AS r', array('image'))
                        ->join('resort_language AS rl', 'rl.resort_id = r.id', array('name'))
                        ->where('r.id = ?', $type_id);
                    break;
                }
            }
            $sql->where('language_id = ?', 5);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getTypeById($type)
        {
            switch($type) {
                case    1   :   $result = 'Khách sạn'; break;
                case    2   :   $result = 'Du thuyền'; break;
                case    3   :   $result = 'Nghỉ dưỡng an lạc'; break;
            }
            return $result;
        }
        
        public function addSales($data)
        {
            $this->_db->insert('sales',$data);
            return $this->_db->lastInsertId();
        }
        
        public function addSalesLanguage($data)
        {
            $this->_db->insert('sales_language',$data);
        }
        
        public function deleteSales($id)
        {
            $id = (int) $id;
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->delete('sales', $where);
            $where = $this->getAdapter()->quoteInto('sales_id = ?', $id);
            $this->_db->delete('sales_language', $where);
        }
        
        public function deleteMultiSales($listId)
        {
            foreach($listId as $id) {
                $this->deleteSales($id);
            }
        }
        
        public function updateSales($data, $id)
        {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->update('sales', $data, $where);
        }
        
        public function updateSalesLanguage($data,$id)
        {
            $where = 'sales_id = '.$id.' AND language_id = '.$data['language_id'];
            $this->_db->update('sales_language', $data, $where);
        }
        
        public function searchSales($data)
        {
            $sql = $this->_db->select()
		                    ->from('sales')
                            ->order('id DESC');
		    if($data['sales'] != '') {
		        $sql = $sql->where('sales LIKE ?', $data['sales']);
		    }
            if($data['type'] != '') {
                $sql = $sql->where('type LIKE ?', $data['type']);
            }
		    if($data['status'] != 0) {
		        $sql = $sql->where('status = ?', $data['status']);
		    }
		    return $sql;
        }
    }
